package com.young.manager.controller;

import com.young.common.interf.IBaseController;
import com.young.common.model.ResultData;
import com.young.manager.model.CompanyUser;
import com.young.manager.service.CompanyUserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("companyUser")
@RestController
@Slf4j
@RequiredArgsConstructor
public class CompanyUserController implements IBaseController {

  private final CompanyUserService companyUserService;

  @GetMapping
  ResultData<?> queryList(
      @RequestParam(required = false) Long companyId,
      @RequestParam(required = false) String name,
      @RequestParam(required = false) String phone,
      @RequestParam(required = false) Integer status,
      @RequestParam(required = false) Integer pageNum,
      @RequestParam(required = false) Integer pageSize
  ) {
    pageNum = null == pageNum ? DEFAULT_PAG_ENUM : pageNum;
    pageSize = null == pageSize ? DEFAULT_PAGE_SIZE : pageSize;
    return success(companyUserService.queryPage(companyId, name, phone, status, pageNum, pageSize));
  }

  @PostMapping("insert")
  ResultData<?> insertCompanyUser(@RequestBody CompanyUser companyUser) {
    companyUserService.insertCompanyUser(companyUser);
    return success();
  }

  @PostMapping("update")
  ResultData<?> updateCompanyUser(@RequestBody CompanyUser companyUser) {
    companyUserService.updateCompanyUser(companyUser);
    return success();
  }

  @PostMapping("updateStatus")
  ResultData<?> updateCompanyUserStatus(@RequestParam Long id, @RequestParam Integer status) {
    companyUserService.updateCompanyUserStatus(id, status);
    return success();
  }

  @PostMapping("resetPassword")
  ResultData<?> resetPassword(@RequestParam Long id, @RequestParam(required = false) String newPassword) {
    companyUserService.resetPassword(id, newPassword);
    return success();
  }

  @PostMapping("delete")
  ResultData<?> deleteCompanyUser(@RequestParam Long id) {
    companyUserService.deleteCompanyUser(id);
    return success();
  }
}
